package com.waidi.clothespad.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.waidi.clothespad.R;
import com.waidi.clothespad.common.Config;
import com.waidi.clothespad.common.DevData;
import com.waidi.clothespad.common.MessageToUi;
import com.waidi.clothespad.common.MyToast;
import com.waidi.clothespad.common.VerData;
import com.waidi.clothespad.common.WorkerInfo;
import com.waidi.clothespad.mqttclient.MyMqttClient;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/4.
 */

public class WorkerinfoDialog extends Dialog{
    private View view;
    private static final String TAG = "WorkerinfoDialog";
    private Context context;
    private Handler handler0;
    TextView tv_workername ;
    TextView tv_workerid ;
    TextView tv_line ;
    TextView tv_time;
    Button bt_logout;

    public WorkerinfoDialog(@NonNull Context context,Handler handlerin) {
        super(context);
        this.context = context;
        this.handler0 = handlerin;
        init();
    }
    public WorkerinfoDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //init();
    }

    private void init() {

        View view = LayoutInflater.from(context).inflate(R.layout.layer_worker_info ,null);
        tv_workername = (TextView) view.findViewById(R.id.tv_workername);
        tv_workerid = (TextView) view.findViewById(R.id.tv_workerid);
        tv_line = (TextView) view.findViewById(R.id.tv_line);
        tv_time = (TextView) view.findViewById(R.id.tv_time);
        bt_logout = (Button) view.findViewById(R.id.bt_logout);


        bt_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(MyMqttClient.myClientConn==false)
                {
                    MyToast.showToast(context,"mqtt服务未连接,无法在终端上进行用户注销，请尝试在吊挂软件上进行注销");
                    return;
                }
                //开启一个线程处理耗时的操作
                new Thread(new Runnable() {
                    @Override
                    public void run()
                    {
                        MyMqttClient.workerSyncToDg(0,Config.rfidWorkerInfo.workernum);
                    }
                }).start();

                Config.rfidWorkerInfo.workernum = "";
                Config.rfidWorkerInfo.workername="未登录";
                Config.rfidWorkerInfo.line="";
                Config.rfidWorkerInfo.logtime="0000-00-00 00:00";
                Config.rfidworkerinfosave();
               // Config.Workerinfosyncflag=1;

                new MessageToUi().send(120,"info","logout",handler0);
                MyToast.showToast(context,"注销成功！");
                dismiss();
            }
        });

        tv_workername.setText(Config.rfidWorkerInfo.workername);
        tv_workerid.setText(Config.rfidWorkerInfo.workernum);
        tv_line.setText(Config.rfidWorkerInfo.line);
        tv_time.setText(Config.rfidWorkerInfo.logtime);


        setContentView(view);
        this.getWindow().setDimAmount(Config.transparency);
        Window dialogWindow = getWindow();
        WindowManager manager = ((Activity) context).getWindowManager();
        WindowManager.LayoutParams params = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        dialogWindow.setGravity(Gravity.CENTER);//设置对话框位置
        Display d = manager.getDefaultDisplay(); // 获取屏幕宽、高度
        params.width = (int) (d.getWidth() * 0.45); // 宽度设置为屏幕的0.65，根据实际情况调整
        params.height = (int) (d.getHeight() * 0.40); // 宽度设置为屏幕的0.65，根据实际情况调整
        dialogWindow.setAttributes(params);
    }

    public void iniData(WorkerInfo tmp)
    {
       try
       {
            tv_workername.setText(tmp.name);
            tv_workerid.setText(tmp.id);
            tv_line.setText(tmp.line);
            tv_time.setText(tmp.logontime);
            Config.rfidWorkerInfo.workername = tmp.name;
            Config.rfidWorkerInfo.workernum = tmp.id;
            Config.rfidWorkerInfo.logtime = tmp.logontime;
            Config.rfidWorkerInfo.line = tmp.line;
            Config.rfidworkerinfosave();
       }
       catch (Exception e)
       {

       }
    }

}
